#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

case "${OSTYPE}" in
    msys*) python="winpty python";;
    *) python="python";;
esac

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

venv() {
    env_dir=./third_party/env/tf
    [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
    case "${OSTYPE}" in
        msys*) source ${env_dir}/Scripts/activate;;
        *) source ${env_dir}/bin/activate;;
    esac
    ${python} -m pip install --quiet --upgrade pip
}

clean() {
    bold "tf clean"
    rm -rf "./third_party/env/tf"
    rm -rf "./third_party/source/tf"
    rm -rf "./third_party/source/tflite-support"
}

sync() {
    bold "tf sync"
    [ -d "./third_party/source/tf" ] || git clone --quiet --recursive https://github.com/tensorflow/tensorflow.git "./third_party/source/tf"
    pushd "./third_party/source/tf" > /dev/null
    git pull --quiet --prune
    git submodule sync --quiet
    git submodule update --quiet --init --recursive
    popd > /dev/null
    [ -d "./third_party/source/tflite-support" ] || git clone --quiet --recursive https://github.com/tensorflow/tflite-support.git "./third_party/source/tflite-support"
    pushd "./third_party/source/tflite-support" > /dev/null
    git pull --quiet --prune
    popd > /dev/null
}

install() {
    bold "tf install"
    if [ "$(${python} -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')" = "3.9" ]; then
        echo "- python 3.9 [skip]"
        return
    fi
    if [ "$(uname -sm)" = "Darwin arm64" ]; then
        echo "- arm64 [skip]"
        return
    fi
    venv
    ${python} -m pip install --quiet --upgrade wheel
    ${python} -m pip install --quiet --upgrade tf-nightly
    deactivate
}

schema() {
    bold "tf schema"
    [[ $(grep -U $'\x0D' ./source/tf-proto.js) ]] && crlf=1
    node ./tools/protoc.js --text --root tf --out ./source/tf-proto.js --path ./third_party/source/tf tensorflow/core/protobuf/saved_model.proto tensorflow/core/protobuf/tensor_bundle.proto tensorflow/core/util/saved_tensor_slice.proto tensorflow/core/util/event.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/tf-proto.js ./source/tf-proto.js
    fi
    bold "tflite schema"
    [[ $(grep -U $'\x0D' ./source/tflite-schema.js) ]] && crlf=1
    temp1=$(mktemp -d)
    temp2=$(mktemp -d)
    node ./tools/flatc.js --text --root tflite --out ./source/tflite-schema.js ./third_party/source/tf/tensorflow/lite/schema/schema.fbs
    node ./tools/flatc.js --root tflite --out ${temp1}/tflite-metadata-schema.js ./third_party/source/tflite-support/tensorflow_lite_support/metadata/metadata_schema.fbs
    sed "s/var \$root = flatbuffers.get('tflite');//g" < ${temp1}/tflite-metadata-schema.js >> ${temp2}/tflite-metadata-schema.js
    cat ${temp2}/tflite-metadata-schema.js >> ./source/tflite-schema.js
    rm -rf ${temp1} ${temp2}
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/tflite-schema.js ./source/tflite-schema.js
    fi
}

metadata() {
    bold "tf metadata"
    if [ "$(${python} -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')" = "3.9" ]; then
        echo "- python 3.9 [skip]"
        return
    fi
    if [ "$(uname -sm)" = "Darwin arm64" ]; then
        echo "- arm64 [skip]"
        return
    fi
    venv
    if [[ $(grep -U $'\x0D' ./source/tf-metadata.json) ]]; then crlf=1; else crlf=; fi
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    ${python} ./tools/tf-script.py metadata
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/tf-metadata.json ./source/tf-metadata.json
    fi
    deactivate
    bold "keras metadata"
    venv
    if [[ $(grep -U $'\x0D' ./source/keras-metadata.json) ]]; then crlf=1; else crlf=; fi
    ${python} ./tools/keras-script.py metadata
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/keras-metadata.json ./source/keras-metadata.json
    fi
    deactivate
}

zoo() {
    bold "keras zoo"
    venv
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    ${python} ./tools/keras-script.py zoo
    deactivate
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "install") install;;
        "schema") schema;;
        "metadata") metadata;;
        "zoo") zoo;;
    esac
done
